﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HslCommunication;
using HslCommunication.ModBus;

namespace YJYpublicClass
{
    public class CalModbus
    {
        HslCommunication.ModBus.ModbusTcpServer modbus;
        private bool Isconnceted = false;

        private int connectTimeOut = 10000;
        public int ConnectTimeOut
        {
            get
            {
                return connectTimeOut;
            }

            set
            {
                connectTimeOut = value;
            }
        }


        private int receiveTimeOut = 5000;
        public int ReceiveTimeOut
        {
            get
            {
                return receiveTimeOut;
            }
            set
            {
                receiveTimeOut = value;
            }
        }

        private HslCommunication.Core.DataFormat dataFormat = HslCommunication.Core.DataFormat.CDAB;
        public HslCommunication.Core.DataFormat DataFormat
        {
            get
            {
                return dataFormat;
            }
            set
            {
                dataFormat = value;
            }
        }

        public bool Connect(int port)
        {
            modbus = new HslCommunication.ModBus.ModbusTcpServer();
            modbus.Station = 1;
            modbus.IsStringReverse = false;
            modbus.DataFormat = HslCommunication.Core.DataFormat.CDAB;
            modbus.ServerStart(port);

            Isconnceted = true;

            return Isconnceted;
        }

        public void Close()
        {
            modbus.ServerClose();
            modbus.Dispose();
            Isconnceted = false;
        }


        public int ReadInt(string adress)
        {
            OperateResult Res = new OperateResult();
            OperateResult<int> ss = new OperateResult<int>();
            for (int i = 0; i < 3; i++)
            {
                int IntAdress = int.Parse(adress);
                ss = modbus.ReadInt32(IntAdress.ToString());
                if (ss.IsSuccess)
                {
                    break;
                }
                else
                {
                    modbus.ServerClose();
                    modbus.ServerStart(502);
                }
            }
            return ss.Content;
        }


        public bool WriteInt(string adress, int value)
        {
            OperateResult Res = new OperateResult();
            for (int i = 0; i < 3; i++)
            {
                int IntAdress = int.Parse(adress);
                Res = modbus.Write(IntAdress.ToString(), value);
                if (Res.IsSuccess)
                {
                    break;
                }
                else
                {
                    modbus.ServerClose();
                    modbus.ServerStart(502);
                }
            }
            return Res.IsSuccess;

        }


        public bool WriteShort(string adress, short value)
        {
            OperateResult Res = new OperateResult();
            for (int i = 0; i < 3; i++)
            {
                int IntAdress = int.Parse(adress);
                Res = modbus.Write(IntAdress.ToString(), value);
                if (Res.IsSuccess)
                {
                    break;
                }
                else
                {
                    modbus.ServerClose();
                    modbus.ServerStart(502);
                }
            }
            return Res.IsSuccess;

        }


        public double ReadDouble(string adress)
        {
            OperateResult Res = new OperateResult();
            OperateResult<double> ss = new OperateResult<double>();
            for (int i = 0; i < 3; i++)
            {
                int IntAdress = int.Parse(adress);
                ss = modbus.ReadDouble(IntAdress.ToString());
                if (ss.IsSuccess)
                {
                    break;
                }
                else
                {
                    modbus.ServerClose();
                    modbus.ServerStart(502);
                }
            }
            return ss.Content;
        }

        public bool WriteDouble(string adress, double value)
        {
            OperateResult Res = new OperateResult();
            for (int i = 0; i < 3; i++)
            {
                int IntAdress = int.Parse(adress);
                Res = modbus.Write(IntAdress.ToString(), value);
                if (Res.IsSuccess)
                {
                    break;
                }
                else
                {
                    modbus.ServerClose();
                    modbus.ServerStart(502);
                }
            }
            return Res.IsSuccess;

        }


        public float ReadFloat(string adress)
        {
            OperateResult Res = new OperateResult();
            OperateResult<float> ss = new OperateResult<float>();
            for (int i = 0; i < 3; i++)
            {
                int IntAdress = int.Parse(adress);
                ss = modbus.ReadFloat(IntAdress.ToString());
                if (ss.IsSuccess)
                {
                    break;
                }
                else
                {
                    modbus.ServerClose();
                    modbus.ServerStart(502);
                }
            }
            return ss.Content;
        }

        public bool WriteFloat(string adress, float value)
        {
            OperateResult Res = new OperateResult();
            for (int i = 0; i < 3; i++)
            {
                int IntAdress = int.Parse(adress);
                Res = modbus.Write(IntAdress.ToString(), value);
                if (Res.IsSuccess)
                {
                    break;
                }
                else
                {
                    modbus.ServerClose();
                    modbus.ServerStart(502);
                }
            }
            return Res.IsSuccess;

        }

        public bool IsConnect
        {
            set
            {
                Isconnceted = value;
            }
            get { return Isconnceted; }
        }
    }
}
